package com.maimang.platform.crm.utils.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Vector;

/**
 * @Author jintaozhao@qq.com
 * @Date 2017/9/18 15:45
 * @project common-common
 * @Package com.maimang.platform.utilss
 * @Description HTTP请求对象工具类
 */
public class HttpRequester {

	/**
	 * @author zjt
	 * @date 2017/9/18
	 * @description HTTP请求响应对象
	 */
	public class HttpResponse {
		private String urlString;
		private int defaultPort;
		private String file;
		private String host;
		private String path;
		private int port;
		private String query;
		private String ref;
		private String userInfo;
		private String contentEncoding;
		private String content;
		private String contentType;
		private String messgae;
		private String method;
		private int code;
		private int connectTimeout;

		public String getProtocol() {
			return protocol;
		}

		public void setProtocol(String protocol) {
			this.protocol = protocol;
		}

		private int readTimeout;
		private String protocol;

		private Vector<String> contentCollection;

		public String getUrlString() {
			return urlString;
		}

		public void setUrlString(String urlString) {
			this.urlString = urlString;
		}

		public int getDefaultPort() {
			return defaultPort;
		}

		public void setDefaultPort(int defaultPort) {
			this.defaultPort = defaultPort;
		}

		public String getFile() {
			return file;
		}

		public void setFile(String file) {
			this.file = file;
		}

		public String getHost() {
			return host;
		}

		public void setHost(String host) {
			this.host = host;
		}

		public String getPath() {
			return path;
		}

		public void setPath(String path) {
			this.path = path;
		}

		public int getPort() {
			return port;
		}

		public void setPort(int port) {
			this.port = port;
		}

		public String getQuery() {
			return query;
		}

		public void setQuery(String query) {
			this.query = query;
		}

		public String getRef() {
			return ref;
		}

		public void setRef(String ref) {
			this.ref = ref;
		}

		public String getUserInfo() {
			return userInfo;
		}

		public void setUserInfo(String userInfo) {
			this.userInfo = userInfo;
		}

		public String getContentEncoding() {
			return contentEncoding;
		}

		public void setContentEncoding(String contentEncoding) {
			this.contentEncoding = contentEncoding;
		}

		public String getContent() {
			return content;
		}

		public void setContent(String content) {
			this.content = content;
		}

		public String getContentType() {
			return contentType;
		}

		public void setContentType(String contentType) {
			this.contentType = contentType;
		}

		public String getMessgae() {
			return messgae;
		}

		public void setMessgae(String messgae) {
			this.messgae = messgae;
		}

		public String getMethod() {
			return method;
		}

		public void setMethod(String method) {
			this.method = method;
		}

		public int getCode() {
			return code;
		}

		public void setCode(int code) {
			this.code = code;
		}

		public int getConnectTimeout() {
			return connectTimeout;
		}

		public void setConnectTimeout(int connectTimeout) {
			this.connectTimeout = connectTimeout;
		}

		public int getReadTimeout() {
			return readTimeout;
		}

		public void setReadTimeout(int readTimeout) {
			this.readTimeout = readTimeout;
		}

		public Vector<String> getContentCollection() {
			return contentCollection;
		}

		public void setContentCollection(Vector<String> contentCollection) {
			this.contentCollection = contentCollection;
		}
	}

	private String defaultContentEncoding;

	public HttpRequester() {
		this.defaultContentEncoding = Charset.defaultCharset().name();
	}

	public HttpResponse sendGet(String urlString) throws IOException {
		return this.send(urlString, "GET", null, null);
	}

	public HttpResponse sendGet(String urlString, Map<String, String> params) throws IOException {
		return this.send(urlString, "GET", params, null);
	}

	public HttpResponse sendGet(String urlString, Map<String, String> params, Map<String, String> properties) throws IOException {
		return this.send(urlString, "GET", params, properties);
	}

	public HttpResponse sendPost(String urlString) throws IOException {
		return this.send(urlString, "POST", null, null);
	}

	public HttpResponse sendPost(String urlString, Map<String, String> params) throws Exception {
		return this.send(urlString, "POST", params, null);
	}

	public HttpResponse sendPost(String urlString, Map<String, String> params, Map<String, String> properties) throws IOException {
		return this.send(urlString, "POST", params, properties);
	}

	private HttpResponse send(String urlString, String method, Map<String, String> parameters, Map<String, String> properties) throws IOException {
		HttpURLConnection urlConnection = null;
		if (method.equalsIgnoreCase("GET") && parameters != null) {
			StringBuffer param = new StringBuffer();
			int i = 0;
			for (String key : parameters.keySet()) {
				if (i == 0) {
					param.append("?");
				} else {
					param.append("&");
				}
				param.append(key).append("=").append(parameters.get(key));
			}
			urlString += param;
		}
		URL url = new URL(urlString);
		urlConnection = (HttpURLConnection) url.openConnection();
		urlConnection.setRequestMethod(method);
		urlConnection.setDoOutput(true);
		urlConnection.setDoInput(true);
		urlConnection.setUseCaches(false);
		if (properties != null) {
			for (String key : properties.keySet()) {
				urlConnection.addRequestProperty(key, properties.get(key));
			}
		}
		if (method.equalsIgnoreCase("POST") && parameters != null) {
			StringBuffer param = new StringBuffer();
			for (String key : parameters.keySet()) {
				param.append("&");
				param.append(key).append("=").append(parameters.get(key));
			}
			urlConnection.getOutputStream().write(param.toString().getBytes());
			urlConnection.getOutputStream().flush();
			urlConnection.getOutputStream().close();
		}
		return this.makeContent(urlString, urlConnection);
	}

	private HttpResponse makeContent(String urlString, HttpURLConnection urlConnection) throws IOException {
		HttpResponse httpResponse = new HttpResponse();
		try {
			String encoding = urlConnection.getContentEncoding();
			if (encoding == null)
				encoding = this.defaultContentEncoding;
			InputStream inputStream = urlConnection.getInputStream();
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, encoding));
			httpResponse.contentCollection = new Vector<String>();
			StringBuffer temp = new StringBuffer();
			String line = bufferedReader.readLine();
			while (line != null) {
				httpResponse.contentCollection.add(line);
				temp.append(line).append("\r\n");
				line = bufferedReader.readLine();
			}
			bufferedReader.close();

			httpResponse.urlString = urlString;
			httpResponse.defaultPort = urlConnection.getURL().getDefaultPort();
			httpResponse.file = urlConnection.getURL().getFile();
			httpResponse.host = urlConnection.getURL().getHost();
			httpResponse.path = urlConnection.getURL().getPath();
			httpResponse.port = urlConnection.getURL().getPort();
			httpResponse.protocol = urlConnection.getURL().getProtocol();
			httpResponse.query = urlConnection.getURL().getQuery();
			httpResponse.ref = urlConnection.getURL().getRef();
			httpResponse.userInfo = urlConnection.getURL().getUserInfo();

			httpResponse.content = temp.toString();
			httpResponse.contentEncoding = encoding;
			httpResponse.code = urlConnection.getResponseCode();
			httpResponse.messgae = urlConnection.getResponseMessage();
			httpResponse.contentType = urlConnection.getContentType();
			httpResponse.method = urlConnection.getRequestMethod();
			httpResponse.connectTimeout = urlConnection.getConnectTimeout();
			httpResponse.readTimeout = urlConnection.getReadTimeout();
			return httpResponse;
		} catch (IOException e) {
			throw e;
		} finally {
			if (urlConnection != null) {
				urlConnection.disconnect();
			}
		}
	}
}

